package com.example.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class StdxTest {
    public static void main(String[] args) {
        List<SzdxlxDir> dirs = new ArrayList<>();
        String phpsessid = "elo4eljvrepfh5vgf0cs6i1l44";
        String[] kcdms = new String[]{"BKS002", "BKS006", "P10011", "P10612", "XX1005", "XX1012"};
        for (String kcdm : kcdms) {
            HttpRequest get = HttpUtil.createGet("https://stu.5zk.com.cn/zk8exam/jp_wiki_study.php?kcdm=" + kcdm);
            get.cookie("PHPSESSID=" + phpsessid);
            Document document = Jsoup.parse(get.execute().body());
            System.out.println("获取到 userlog_id：" + document.getElementById("userlog_id").val());
            String typeId = document.getElementById("TypeId").val();
            System.out.println("获取到 typeId：" + typeId);

            Elements elements = document.select(" .block-content ul li ");
            for (Element element : elements) {
                String attr = element.selectFirst("a").attr("href");
                String jjwikiid = StrUtil.subAfter(attr, "=", true);
//            System.out.println("视频id：" + jjwikiid);

                String div = element.select("div").get(1).text();

                String[] curTimeArr = StrUtil.subBetween(div, "(", "/").split(":");
                int curTotal = Integer.parseInt(curTimeArr[0]) * 3600 + Integer.parseInt(curTimeArr[1]) * 60 + Integer.parseInt(curTimeArr[2]);
//            System.out.println("获取到当前观看视频时长：" + curTotal);

                String[] timeArr = StrUtil.subBetween(div, "/", ")").split(":");
                int total = Integer.parseInt(timeArr[0]) * 3600 + Integer.parseInt(timeArr[1]) * 60 + Integer.parseInt(timeArr[2]);
//            System.out.println("获取到视频时长：" + total);

                SzdxlxDir szdxlxDir = new SzdxlxDir();
                szdxlxDir.setKcdm(kcdm);
                szdxlxDir.setJjwikiid(jjwikiid);
                szdxlxDir.setCurSj(curTotal);
                szdxlxDir.setMaxSj(total);
                szdxlxDir.setTypeId(typeId);
                dirs.add(szdxlxDir);
            }
        }
        System.out.println(JSON.toJSONString(dirs, JSONWriter.Feature.PrettyFormat));
    }
}
